//@author A0088498A
package logic;

import gui.IUserInterface;

import java.util.HashMap;
import java.util.List;

import common.AttributeType;
import common.CommandType;
import common.Task;

/**
 * DeleteCommandHandler class<br/>
 * This class contains all the logic for Delete command, which will delete a
 * Task into the database</br>
 */

public class DeleteCommandHandler extends CommandHandler{
	private final String DELETE_SUCESSFUL = "\n\nTask is deleted successfully!\n%1$s";
	private final String DELETE_ERROR = "\n\nError occured while deleting\n%1$s";
	private final String PROMPT_MESSAGE = "\n\nWhich task do you want to delete?\n";
	private final String SYNTAX_ERROR = "Delete Command syntax error. Press F10 for help\n";

	public DeleteCommandHandler(IUserInterface ui, String originalCommand, HashMap<AttributeType, String> attrMapping) {
		super(ui, originalCommand, attrMapping);
	}

	/**
	 * Needs to be explicitly called for the command to be started <br/>
	 * This method will first check for syntax first before executing <br/>
	 */
	public void start() {
		if (isSyntaxCorrect()) {
			execute();
		} else {
			pushToHistoryAsNonExecutableCommand();
			writeToConsole(SYNTAX_ERROR);
		}
	}

	/**
	 * Check for requirement for delete task: the name field is compulsory <br/>
	 * 
	 * @return true if the syntax is correct, otherwise false (the name field is
	 *         missing)
	 */
	private boolean isSyntaxCorrect() {
		if (_attrList.get(AttributeType.name) == null) {
			return false;
		}
		return true;
	}
	
	//@author A0097972J
	/**
	 * Execute the command by first searching for all Tasks that have the same
	 * taskName or including the taskName <br/>
	 * If there are more than 1 Task found, ask the Users to pick the Task then
	 * delete <br/>
	 * Otherwise delete the Task <br />
	 */
	public void execute() {
		// Retrieve the list of relative Tasks from storage
		String taskName = _attrList.get(AttributeType.name);
		assert (taskName != null);
		
		List<Task> relevantTasks = searchFromStorage(true, taskName);

		if (relevantTasks != null) {
			if (relevantTasks.size() > 1) {
				// Multiple results
				passResultToConsole(relevantTasks, PROMPT_MESSAGE);
				pushCommandToHistory(CommandType.delete);
				// Save the search result into database for Confirm Command Handler
				getStorage().setPreviousSearchResult(relevantTasks);
				
			} else if (relevantTasks.size() == 1) {
				// Exactly one result found
				Task deleteTask = relevantTasks.get(0);
				deleteFromStorage(deleteTask);
				
			} else {
				// No result
				writeToConsole(NO_RESULT);
			}
		} else {
			writeToConsole(NO_RESULT);
		}
	}

	/**
	 * Delete task from storage <br/>
	 * and record the Delete command to the History <br/>
	 * 
	 * @param deleteTask
	 */
	protected void deleteFromStorage(Task deleteTask) {
		int task_id = deleteTask.getTaskId();

		boolean isDeleted = getStorage().deleteTask(task_id);

		if (isDeleted) {
			String deleteSuccessful = String.format(DELETE_SUCESSFUL, deleteTask.toString(true));
			writeToConsole(deleteSuccessful);
			pushCommandToHistory(CommandType.delete, deleteTask);
		} else {
			String deleteError = String.format(DELETE_ERROR, deleteTask.toString(true));
			writeToConsole(deleteError);
		}
	}

}
